/*
 * Copyright (c) 2010 Mathew Hall, University of Sheffield.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following conditions
 * are met:
 *
 * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following
 * disclaimer in the documentation and/or other materials provided
 * with the distribution.
 *
 * Neither the name of the University of Sheffield nor the names of its
 * contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
package bunchbridge;

import bunch.Node;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import primitives.cluster.ClusterHead;
import primitives.cluster.IClusterLevel;

/**
 * Converts a primivies.Graph to a bunch.Graph
 * 
 * @author mat
 * 
 */
public abstract class GraphConverter {

	/**
	 * Converts an Augen graph to a Bunch graph. This allows Bunch fitness
	 * functions to be applied to Augen graphs so Bunch FFs are usable straight
	 * into bunch.
	 * 
	 * @param g
	 *            the graph to convert
	 * @return a Bunch graph
	 * @see bunch.Graph
	 * @see primitives.Graph
	 */
	public static bunch.Graph convertToBunchGraph(primitives.graph.Graph g) {
		bunch.Graph newGraph = new bunch.Graph(g.getNodes().size());
		HashMap<primitives.graph.Node, Integer> names = new HashMap<primitives.graph.Node, Integer>();
		int i = 0;
		for (primitives.graph.Node n : g.getNodes()) {
			names.put(n, i);
			i++;
		}
		ArrayList<bunch.Node> nodeList = new ArrayList<bunch.Node>();
		for (primitives.graph.Node n : g.getNodes()) {
			HashMap<Integer, Integer> weights = new HashMap<Integer, Integer>();
			for (primitives.graph.Node dest : n.getTransitionsAsN()) {
				Integer number = names.get(dest);
				if (weights.containsKey(number)) {
					weights.put(number, weights.get(number) + 1);
				} else {
					weights.put(number, 1);
				}
			}
			int[] deps = new int[weights.keySet().size()];
			int j = 0;
			int ws[] = new int[weights.keySet().size()];
			for (Integer num : weights.keySet()) {
				deps[j] = num;
				ws[j] = weights.get(num);
				j++;
			}

			bunch.Node newNode = new bunch.Node(n.getLabel(), deps, ws);
			newNode.nodeID = names.get(n);
			nodeList.add(newNode);
		}
		newGraph.clear(); // bunch does this, I haven't got a clue why.
		newGraph.setNodes(nodeList.toArray(new Node[nodeList.size()]));
		return newGraph;
	}

	/**
	 * Convert an Augen clustering to a Bunch one. This can be used to produce a
	 * Bunch ObjectiveFunctionCalculator-friendly version of a clustering
	 * 
	 * @param ch
	 *            a clustering produced by Augen
	 * @return a Bunch representation of it for use with Bunch fitness functions
	 */
	public static bunch.Graph convertToBunchCluster(ClusterHead ch) {
		bunch.Graph retGraph = convertToBunchGraph(new primitives.graph.Graph(ch
				.getNodes()));
		HashMap<String, Integer> names = new HashMap<String, Integer>();
		for (bunch.Node nod : retGraph.getNodes()) {
			names.put(nod.getName(), nod.getId());
		}
		int[] clusters = retGraph.getClusters();
		// todo figure out how this works,
		// is it node number and cluster number? think so.
		// cluster vector looks like
		// [node1clusternumber,node2clusternumber,node3clusternumber] etc
		int i = 0;
		for (IClusterLevel child : ch.getChildren()) {
                        if(child.getNodes().isEmpty())
                            continue;
			for (primitives.graph.Node clusterChild : child.getNodes()) {
				clusters[names.get(clusterChild.getLabel())] = i;
			}
			i++;
		}

		return retGraph;
	}
}
